<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <input type="file" hidden ref="file" @change="onFileChange" />

    <!-- 个人信息 -->
    <van-cell
      @click="$refs.file.click()"
      class="avatar-cell"
      title="头像"
      is-link
      center
    >
      <van-image class="avatar" round fit="cover" :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      @click="isUpdateNameShow = true"
      is-link
    />
    <van-cell
      @click="isUpdateGenderShow = true"
      title="性别"
      :value="user.gender === 1 ? '女' : '男'"
      is-link
    />
    <van-cell
      @click="isUpdateBirthdayShow = true"
      title="生日"
      :value="user.birthday"
      is-link
    />
    <!-- /个人信息 -->

    <!-- 编辑昵称 -->
    <van-popup
      style="height: 100%"
      v-model="isUpdateNameShow"
      position="bottom"
    >
      <UpdateName
        v-if="isUpdateNameShow"
        @close="isUpdateNameShow = false"
        v-model="user.name"
      />
    </van-popup>
    <!-- /编辑昵称 -->
    <!-- 编辑性别 -->
    <van-popup
      style="height: 40%"
      v-model="isUpdateGenderShow"
      position="bottom"
    >
      <UpdateGender @close="isUpdateGenderShow = false" v-model="user.gender" />
    </van-popup>
    <!-- /编辑性别 -->
    <!-- 编辑生日 -->
    <van-popup
      style="height: 40%"
      v-model="isUpdateBirthdayShow"
      position="bottom"
    >
      <UpdateBirthday
        v-model="user.birthday"
        @close="isUpdateBirthdayShow = false"
      />
    </van-popup>
    <!-- /编辑生日 -->
    <!-- 编辑头像 -->
    <van-popup
      style="height: 100%"
      v-model="isUpdateAvantarShow"
      position="bottom"
    >
      <UpdatePhoto
        v-if="isUpdateAvantarShow"
        :img="img"
        @close="isUpdateAvantarShow = false"
        @update-photo="user.photo = $event"
      />
    </van-popup>
    <!-- /编辑头像 -->
  </div>
</template>

<script>
import UpdateName from './components/update-name.vue';
import UpdateGender from './components/update-gender.vue';
import UpdateBirthday from './components/update-birthday.vue';
import UpdatePhoto from './components/update-photo.vue';
import { getUserProfile } from '@/api/user';
export default {
  name: 'UserProfile',
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  props: {},
  data () {
    return {
      user: {},
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdateAvantarShow: false,
      img: null
    };
  },
  computed: {},
  watch: {},
  created () {
    this.loadUserProfile();
  },
  mounted () {},
  methods: {
    async loadUserProfile () {
      try {
        const { data } = await getUserProfile();
        // console.log(data);
        this.user = data.data;
      } catch (error) {
        this.$toast('用户信息请求失败请稍后重试');
      }
    },
    onFileChange () {
      // 获取文件对象
      const file = this.$refs.file.files[0];
      // 基于文件获取图片数据
      this.img = window.URL.createObjectURL(file);
      this.isUpdateAvantarShow = true;
      this.$refs.file.value = '';
    }
  }
};
</script>

<style scoped lang="less">
.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
    .avatar {
      width: 60px;
      height: 60px;
    }
  }
}
</style>
